$().ready(function(){

    $baseUrl=$("#baseUrl").text();   
    
    /* Method for checking if the email is taken for #email field */
    $.validator.addMethod("emailTaken", function(value, element)  {
    	var $returnValue;
    	
    	$.ajax({
    		  url: $baseUrl+ '/user/checkemailajax',
    		  type: 'post',
    		  async: false,
    		  data: ({email : value}),
    		  success: function(data) {
	 		  $returnValue = (data!=1);
	 		   return;
    		  }});   	
    	
    	return $returnValue;
	 	
    }, 'Email is already taken!');  
    
    $.validator.addMethod("selectCountryNull", function(value, element)  {
    	if ($(element).parent().css("display")!="none")
    		return value != 0;
    	else
    		return true;
    }, 'Please select one country ...');  

    $.validator.addMethod("selectCityNull", function(value, element)  {
    	if ($(element).parent().css("display")!="none")
    		return value != 0;
    	else
    		return true;
    }, 'Please select one ciity ...'); 
    
    $.validator.addMethod("selectRegionNull", function(value, element)  {
    	if ($(element).parent().css("display")!="none")
    		return value != 0;
    	else
    		return true;
    }, 'Please select one region ...'); 

    /* ajax checking for login form */
    $("#loginForm").submit(function(){
    	//var $returnValue = true;
    	var $next = $("#next").val() == "" ? "/" : $("#next").val();
    	$.ajax({
    		  url: $baseUrl+ '/user/loginajax',
    		  type: 'post',
    		  async: false,
    		  data: ({email : $("#email").val(), password : $("#password").val()}),
    		  success: function(data) {
    				$("#password-element div.error").remove();
    			if (data!=1) {
    				$("#password-element").append("<div class='error'>Email and Password not match, try again?</div>");
    			}else {
        			location.href = $next;
        		}
    			
    		  }});   	
    	
    	return false;
    	
    });   	
    
    $("#registerForm").validate({
    	rules: {
		name: {
			required: true,
			minlength: 2,
			maxlength: 30
		},
		email: {
			required: true,
			email: true,
			emailTaken: true
		},
		password: {
			required: true,
			minlength: 6,
			maxlength: 20
		},
		country:{
			selectCountryNull:true
		},
		city:{
			selectCityNull:true
		},
		region:{
			selectRegionNull:true
		}
	},
	messages: {
		name: {
			required: "How do others call you ...",
			minlength: "That is ... too short .... at least 2?",
			maxlength: "Er ... might be complicated for others to call you?"
		},
		email:{
			required: "Email can't be empty ...",
			email: "The format seems not right -_-||... "
		},
		password: {
			required: "Password can't be empty...",
			minlength: "Too weak, too short ...",
			maxlength: "@_@ You can still remember it ....?"
		}
	}
    });
    
    $("#settingForm").validate({
    	rules: {
		name: {
			required: true,
			minlength: 2,
			maxlength: 30
		},
		country:{
			selectCountryNull:true
		},
		city:{
			selectCityNull:true
		},
		region:{
			selectRegionNull:true
		}
	},
	messages: {
		name: {
			required: "How do others call you ...",
			minlength: "That is ... too short .... at least 2?",
			maxlength: "Er ... might be complicated for others to call you?"
		}
	}
    });
         
    $("#loginForm").validate({
    	rules: {		
		password: {
			required: true
		},
		email: {
			required: true,
			email: true
		}
	},
	messages: {
		email:{
			required: "Email can't be empty ...",
			email: "The format seems not right -_-||... "
		},
		password: {
			required: "Password can't be empty..."
		}
	}
    }); 
    
});